package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.OrderDetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.dao.impl.OrderDetailetIDaompl;
import com.foreknow.elm.dao.impl.OrdersDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.OrderDetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    OrdersDao ordersDao = new OrdersDaoImpl();
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public int sinsert(Orders orders) {
        conn = DBUtil.getConnection();
        int a = 0;
        try {
            DBUtil.beginTransaction();
            CartDao cartDao = new CartDaoImpl();
            Cart cart = new Cart();
            String uid = orders.getUserId();
            Integer bid = orders.getBusinessId();
            cart.setUserId(uid);
            cart.setBusinessId(bid);
            List<Cart> list = cartDao.query(cart);
            List<OrderDetailet> li = new ArrayList<>();
            for (Cart c:list) {
                OrderDetailet orderDetailet = new OrderDetailet();
                orderDetailet.setFoodId(c.getFoodID());
                orderDetailet.setQuantity(c.getQuantity());
                li.add(orderDetailet);
            }
            a = ordersDao.insert(orders);
            //添加明细
            OrderDetailetDao orderDetailetDao = new OrderDetailetIDaompl();
            Integer orderid = orders.getOrderId();
            for (int i = 0;i<li.size();i++){
                OrderDetailet orderDetailet = new OrderDetailet();
                orderDetailet.setOrderId(a);
                orderDetailet.setFoodId(li.get(i).getFoodId());
                orderDetailet.setQuantity(li.get(i).getQuantity());
                int c = orderDetailetDao.save(orderDetailet);
            }
            for (int i = 0 ;i<li.size();i++){
                //删除购物车
                Cart cart1 = new Cart();
                cart1.setUserId(uid);
                cart1.setBusinessId(bid);
                cart1.setFoodID(li.get(i).getFoodId());
                int d = cartDao.delete(cart1);
            }
            DBUtil.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }finally {
            DBUtil.close();
        }
        return a;
    }

    @Override
    public Orders squeryobo(Integer orderId) {
        conn = DBUtil.getConnection();
        Orders orders = new Orders();
        try {
            orders = ordersDao.queryobo(orderId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return orders;
    }

    @Override
    public List<Orders> squeryall(String userId) {
        conn = DBUtil.getConnection();
        List<Orders> list = new ArrayList<>();
        try {
            list = ordersDao.queryall(userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return list;
    }
}



